import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'

function hasUrl(url) {
  return store.getters.getUser.menus_url.some(item => item == url);
}

Vue.use(Router)

let router = new Router({
  mode:'history',
  routes: [
    {
      path: '/index',
      component:()=>import('../pages/index.vue'),
      children:[
        {
          path:'/home',
          component:()=>import('../views/home.vue')
        },
        {
          path:'/menu',
          component:()=>import('../views/menu/menu.vue'),
          name:'菜单管理',
          beforeEnter(to, from, next) {
            hasUrl("/menu") ? next() : next("/home");
          }
        },
        {
          path:'/roler',
          component:()=>import('../views/roler/roler.vue'),
          name:'角色管理',
          beforeEnter(to, from, next) {
            hasUrl("/roler") ? next() : next("/home");
          }
        },
        {
          path:'/manger',
          component:()=>import('../views/manger/manger.vue'),
          name:'管理员管理',
          beforeEnter(to, from, next) {
            hasUrl("/manger") ? next() : next("/home");
          }
        },
        {
          path:'/goodcate',
          component:()=>import('../views/goodcate/goodcate.vue'),
          name:'商品分类',
          beforeEnter(to, from, next) {
            hasUrl("/goodcate") ? next() : next("/home");
          }
        },
        {
          path:'/goodspecs',
          component:()=>import('../views/goodspecs/goodspecs.vue'),
          name:'商品规格',
          beforeEnter(to, from, next) {
            hasUrl("/goodspecs") ? next() : next("/home");
          }
        },
        {
          path:'/goods',
          component:()=>import('../views/goods/goods.vue'),
          name:'商品管理',
          beforeEnter(to, from, next) {
            hasUrl("/goods") ? next() : next("/home");
          }
        },
        {
          path:'/member',
          component:()=>import('../views/member/member.vue'),
          name:'会员管理',
          beforeEnter(to, from, next) {
            hasUrl("/member") ? next() : next("/home");
          }
        },
        {
          path:'/banner',
          component:()=>import('../views/banner/banner.vue'),
          name:'轮播图管理',
          beforeEnter(to, from, next) {
            hasUrl("/banner") ? next() : next("/home");
          }
        },
        {
          path:'/seck',
          component:()=>import('../views/seck/seck.vue'),
          name:'限时秒杀管理',
          beforeEnter(to, from, next) {
            hasUrl("/seck") ? next() : next("/home");
          }
        },
        {
          path:'',
          redirect:'/home'
        }
      ]
    },
    {
      path: '/login',
      component:()=>import('../pages/login.vue')
    },
    {
      path:'*',
      redirect:'/login'
    }
  ]
})

// router.beforeEach((to,from,next)=>{
//   if(to.path=='/login'){
//     next()
//     return
//   }
//   let userInfo = sessionStorage.getItem('userInfo')? JSON.parse(sessionStorage.getItem('userInfo')):{}
//   if(userInfo){
//     next()
//     return
//   }
//   next('/login')
// })

router.beforeEach((to,from,next)=>{
  if(to.path=='/login'){
    next()
    return
  }
  if(store.getters.getUser){
    next()
    return
  }
  next('/login')
})

export default router
